import api from '../../api';
import { message } from 'antd';

export default {
  namespace: 'exchangeAgreement',
  state: {
    portfolioList: '你好王建军', //组合下拉列表
    shortName: [], //短名下拉列表
    counterparty: [], //所有交易对手名单
  },
  effects: {
    *saveInstruction2({ payload }, { call, put }) {
      const response = yield call(api.exchangeAgreement.saveInstruction2, payload);
      if (response && response.success) {
        message.success('保存成功!');
      } else {
        message.error(response.errorMsg || '保存失败!');
      }
      return response;
    },
    //查询债券基础信息
    *queryBondInfo({ payload }, { call, put, select }) {
      const response = yield call(api.exchangeAgreement.queryBondInfo, payload);
      if (response && response.success) {
        return response;
      } else {
        message.error('查询债券基础信息失败');
      }
    },
    //获取到期结算日和实际占款天数
    *calculation({ payload, callback }, { call, put, select }) {
      // let data = {settlementDate:'2020-10-20',occupationDays:'10'}

      // setTimeout(()=>{
      //   callback(data)
      // }, 1000)

      const res = yield call(api.exchangeAgreement.calculation, payload);
      if (res && res.code == 0) {
        callback(res.data);
      } else {
        message.error(res.msg || '查询到期结算日和实际占款天数失败!');
      }
    },

    //根据组合id获取持仓分析信息
    *getPosition({ payload, callback }, { call, put, select }) {
      const res = yield call(api.exchangeAgreement.getPosition, payload);
      if (res && res.code == '0') {
        callback(res.data);
      } else {
        message.error(res.msg || '查询持仓分析信息失败!');
      }
    },

    *getBookBuyBack({ payload, callback }, { call, put, select }) {
      const res = yield call(api.exchangeAgreement.getBookBuyBack, payload);
      if (res && res.code == 0) {
        callback(res.data);
      } else {
        message.error(res.msg || '查询在途回购信息失败!');
      }
    },

    //根据组合id获取在途回购信息
    *getBuyBack({ payload, callback }, { call, put, select }) {
      // setTimeout(()=>{
      //   callback({columns:columns,dataSource:dataSource})
      // }, 1000)

      const res = yield call(api.exchangeAgreement.getBuyBack, payload);
      if (res && res.code == 0) {
        callback(res.data);
      } else {
        message.error(res.msg || '查询在途回购信息失败!');
      }
    },

    //指令下达
    *saveInstruction({ payload, callback }, { call, put, select }) {
      const res = yield call(api.exchangeAgreement.saveInstruction, payload);
      if (res && res.success) {
        message.success('下达成功!');
      } else {
        message.error(res.errorMsg || '下达失败!');
      }

      callback(res);
    },

    //获取所有交易对手
    *queryCounterparty({ payload }, { call, put }) {
      const response = yield call(api.exchangeAgreement.queryCounterparty, payload);
      if (response && response.success) {
        yield put({
          type: 'save',
          payload: {
            counterparty: response.data,
          },
        });
      }
    },
    //簿记下达 新接口
    *saveOrderNew({ payload }, { call, put }){
      // const response = yield call(api.exchangeAgreement.saveOrderNew, payload);
      const response ={
        code:0
      }
      return response;
    },
    //验资验券 新接口
    *onCheck({ payload }, { call, put }){
      // const response = yield call(api.exchangeAgreement.onCheck, payload);
    const response ={code:1}
      return response;
    },
    //验资验券 新接口
    *queryResult({ payload }, { call, put }){
      // const response = yield call(api.exchangeAgreement.queryResult, payload);
    const response ={code:0}
      return response;
    }
  },
  reducers: {
    save(state, { payload }) {
      return { ...state, ...payload };
    },
  },
};
